package com.lbj.dao.impl;

import com.lbj.dao.TopicDao;
import com.lbj.domain.Topic;
import com.lbj.utils.DynamicQuerySql;
import com.lbj.utils.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TopicDaoImpl implements TopicDao {
    JDBCUtil jdbcUtil = new JDBCUtil();
    Connection conn = jdbcUtil.createCon();

    @Override
    public List<Topic> getTopics() {
        String sql =" select * from tbl_topic  ";
        List<Topic> topics = new ArrayList<>();
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                 String id = rs.getString(1);
                 String title = rs.getString(2);
                 String content = rs.getString(3);
                 String status = rs.getString(4);
                 String createTime = rs.getString(5);
                 String createBy = rs.getString(6);
                 int browseCount = rs.getInt(7);
                 int thumbsCount = rs.getInt(8);
                 int commentsCount = rs.getInt(9);
                 String thumbsId = rs.getString(10);
                 String collectionId = rs.getString(11);
                 Topic topic = new Topic(id,title,content,status,createTime,createBy,browseCount,
                         thumbsCount,commentsCount,commentsCount,thumbsId,collectionId);
                 topics.add(topic);
            }
            rs.close();
            pst.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return topics;
    }

    @Override
    public int deleteTopicById(String id) {
        int row = 0;
        String sql = "delete from tbl_topic where id = ?";
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,id);
            row = pst.executeUpdate();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int addTopic(Topic topic) {
        int row = 0;
        String sql = "insert into tbl_topic values(?,?,?,?,?,?,?,?,?,?,?,?) ";
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,topic.getId());
            pst.setString(2,topic.getTitle());
            pst.setString(3,topic.getContent());
            pst.setString(4,topic.getStatus());
            pst.setString(5,topic.getCreateTime());
            pst.setString(6,topic.getCreateBy());
            pst.setInt(7,topic.getBrowseCount());
            pst.setInt(8,topic.getThumbsCount());
            pst.setInt(9,topic.getCollectionCount());
            pst.setInt(10,topic.getCommentCount());
            pst.setString(11,topic.getThumbsId());
            pst.setString(12,topic.getCollectionId());
            row = pst.executeUpdate();

            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;
    }

    @Override
    public List<Topic> getTopicsByCondition(Map<String, String> conditions) {
        List<Topic> topics = new ArrayList<>();
        String sql = DynamicQuerySql.getSql(conditions,"tbl_topic");
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return topics;
    }

    @Override
    public Topic getTopicById(String id) {
        String sql ="select * from tbl_topic where id = ?";
        Topic topic = null;
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,id);
            rs = pst.executeQuery();
            while (rs.next()){
                String title = rs.getString(2);
                String content = rs.getString(3);
                String status = rs.getString(4);
                String createTime = rs.getString(5);
                String createBy = rs.getString(6);
                int browseCount = rs.getInt(7);
                int thumbsCount = rs.getInt(8);
                int collectionCount = rs.getInt(9);
                int commentsCount = rs.getInt(10);
                String thumbsId = rs.getString(11);
                String collectionId = rs.getString(12);

                topic = new Topic(id,title,content,status,createTime,createBy,
                        browseCount,thumbsCount,collectionCount,commentsCount,thumbsId,collectionId);

            }
            rs.close();
            pst.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return topic;
    }

    @Override
    public int updateTopicInfo(Topic topic) {
        int row = 0;
        String sql = "update tbl_topic set title=?,content=?,status=?,createTime=?,createBy=?,browseCount=?,thumbsCount=?,collectionCount=?,commentCount=?,thumbsId=?,collectionId=? where id =?";

        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);

            pst.setString(1,topic.getTitle());
            pst.setString(2,topic.getContent());
            pst.setString(3,topic.getStatus());
            pst.setString(4,topic.getCreateTime());
            pst.setString(5,topic.getCreateBy());
            pst.setInt(6,topic.getBrowseCount());
            pst.setInt(7,topic.getThumbsCount());
            pst.setInt(8,topic.getCollectionCount());
            pst.setInt(9,topic.getCommentCount());
            pst.setString(10,topic.getThumbsId());
            pst.setString(11,topic.getCollectionId());
            pst.setString(12,topic.getId());

            System.out.println(pst);
            row = pst.executeUpdate();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;
    }

    @Override
    public List<Topic> getTopicsTop() {
        String sql =" select * from tbl_topic t where t.status in ('10','11') order by t.status desc ,t.createTime desc ";
        List<Topic> topics = new ArrayList<>();
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                String id = rs.getString(1);
                String title = rs.getString(2);
                String content = rs.getString(3);
                String status = rs.getString(4);
                String createTime = rs.getString(5);
                String createBy = rs.getString(6);
                int browseCount = rs.getInt(7);
                int thumbsCount = rs.getInt(8);
                int commentsCount = rs.getInt(9);
                String thumbsId = rs.getString(10);
                String collectionId = rs.getString(11);
                Topic topic = new Topic(id,title,content,status,createTime,createBy,browseCount,
                        thumbsCount,commentsCount,commentsCount,thumbsId,collectionId);
                topics.add(topic);
            }
            rs.close();
            pst.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return topics;
    }

    @Override
    public List<Topic> pageList(int start, int pageSize) {
        String sql =" select * from tbl_topic t where status not in ('10','11') order by t.status desc ,t.createTime desc Limit ?,? ";
        List<Topic> topics = new ArrayList<>();
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1,start);
            pst.setInt(2,pageSize);
            rs = pst.executeQuery();
            while (rs.next()){
                String id = rs.getString(1);
                String title = rs.getString(2);
                String content = rs.getString(3);
                String status = rs.getString(4);
                String createTime = rs.getString(5);
                String createBy = rs.getString(6);
                int browseCount = rs.getInt(7);
                int thumbsCount = rs.getInt(8);
                int commentsCount = rs.getInt(9);
                String thumbsId = rs.getString(10);
                String collectionId = rs.getString(11);
                Topic topic = new Topic(id,title,content,status,createTime,createBy,browseCount,
                        thumbsCount,commentsCount,commentsCount,thumbsId,collectionId);
                topics.add(topic);
            }
            rs.close();
            pst.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return topics;
    }

    @Override
    public int getTotalCount() {
        String sql =" select count(*) from tbl_topic t where status not in ('10','11')";
        int totalCount = 0;
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            if(rs.next()){
                totalCount = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return totalCount;
    }
}
